package com.wande.dataplatform.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.wande.common.core.domain.R;
import com.wande.common.mybatis.core.page.PageQuery;
import com.wande.common.mybatis.core.page.TableDataInfo;
import com.wande.common.web.core.BaseController;
import com.wande.dataplatform.domain.bo.DataTagBo;
import com.wande.dataplatform.domain.dto.TagRelationDTO;
import com.wande.dataplatform.domain.dto.TagStatisticsDTO;
import com.wande.dataplatform.domain.vo.DataTagVO;
import com.wande.dataplatform.service.IDataTagService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 数据标签控制器
 *
 * @author wande
 */
@RestController
@RequestMapping("/dataplatform/tag")
@RequiredArgsConstructor
public class DataTagController extends BaseController {

    private final IDataTagService dataTagService;

    /**
     * 查询标签树
     */
    @SaCheckPermission("dataplatform:tag:list")
    @GetMapping("/tree")
    public R<List<DataTagVO>> tree(@RequestParam(required = false) Long projectId) {
        List<DataTagVO> tree = dataTagService.selectTagTree(projectId);
        return R.ok(tree);
    }

    /**
     * 查询标签列表
     */
    @SaCheckPermission("dataplatform:tag:list")
    @GetMapping("/list")
    public TableDataInfo<DataTagVO> list(DataTagBo bo, PageQuery pageQuery) {
        return dataTagService.selectTagList(bo, pageQuery);
    }

    /**
     * 查询标签详情
     */
    @SaCheckPermission("dataplatform:tag:query")
    @GetMapping("/{id}")
    public R<DataTagVO> getInfo(@PathVariable Long id) {
        return R.ok(dataTagService.selectTagById(id));
    }

    /**
     * 创建标签
     */
    @SaCheckPermission("dataplatform:tag:add")
    @PostMapping
    public R<Long> add(@Validated @RequestBody DataTagBo bo) {
        Long id = dataTagService.createTag(bo);
        return R.ok(id);
    }

    /**
     * 更新标签
     */
    @SaCheckPermission("dataplatform:tag:edit")
    @PutMapping
    public R<Void> edit(@Validated @RequestBody DataTagBo bo) {
        dataTagService.updateTag(bo);
        return R.ok();
    }

    /**
     * 删除标签
     */
    @SaCheckPermission("dataplatform:tag:remove")
    @DeleteMapping("/{ids}")
    public R<Void> remove(@PathVariable List<Long> ids) {
        dataTagService.deleteTags(ids);
        return R.ok();
    }

    /**
     * 为资产添加标签
     */
    @SaCheckPermission("dataplatform:tag:relation")
    @PostMapping("/relation")
    public R<Void> addTagsToAsset(@Validated @RequestBody TagRelationDTO dto) {
        dataTagService.addTagsToAsset(dto);
        return R.ok();
    }

    /**
     * 移除资产标签
     */
    @SaCheckPermission("dataplatform:tag:relation")
    @DeleteMapping("/relation")
    public R<Void> removeTagsFromAsset(@RequestParam Long assetId,
                                        @RequestParam String assetType,
                                        @RequestParam List<Long> tagIds) {
        dataTagService.removeTagsFromAsset(assetId, assetType, tagIds);
        return R.ok();
    }

    /**
     * 查询资产的标签列表
     */
    @SaCheckPermission("dataplatform:tag:query")
    @GetMapping("/asset")
    public R<List<DataTagVO>> getAssetTags(@RequestParam Long assetId,
                                            @RequestParam String assetType) {
        List<DataTagVO> tags = dataTagService.selectTagsByAsset(assetId, assetType);
        return R.ok(tags);
    }

    /**
     * 自动打标签
     */
    @SaCheckPermission("dataplatform:tag:auto")
    @PostMapping("/auto")
    public R<Void> autoTag(@RequestParam Long assetId,
                           @RequestParam String assetType) {
        dataTagService.autoTagAsset(assetId, assetType);
        return R.ok();
    }

    /**
     * 查询标签统计信息
     */
    @SaCheckPermission("dataplatform:tag:statistics")
    @GetMapping("/statistics")
    public R<List<TagStatisticsDTO>> statistics() {
        List<TagStatisticsDTO> stats = dataTagService.selectTagStatistics();
        return R.ok(stats);
    }
}
